<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Class Mnews extends MY_Model
{
    public function __construct()
    {
        parent::__construct();
    }
    
    public function getNewsDB($number,$offset,$id_parent)
    {
        $id = $this->getParamUri(3,2);
        $limit = $offset ? intval($offset) : '0';
        
        $sql = "SELECT c.img AS cimg, c.title AS ctitle, c.id AS cid, ct.id AS ctid, ct.title AS cttitle, ct.title_ascii AS cttitle_ascii FROM content AS c INNER JOIN categories AS ct ON ct.id = c.id_c WHERE 1=1 AND id_c_parent = ".$id_parent;
        $key = $this->getParamString('keyword');
        if(!in_array($key, array(null, '', '0')))
        {
            $sql .= " AND (c.title like '%".$key."%' OR ";
            $sql .= " c.title_ascii like '%".$key."%' OR ";
            $sql .= " c.title_seo like '%".$key."%' OR ";
            $sql .= " c.title_seo_ascii like '%".$key."%') ";
        }
        $sql .= " ORDER BY c.id DESC";
        
        $xdata = " LIMIT ".$limit.",".$number." ";
        $xs = $this->db->query($sql.$xdata)->result_object();
        $count = $this->db->query($sql)->num_rows();
        return array('data' => $xs, 'count' => $count);
        
    }
    
    public function getCate($id = 0)
    {
        $data = $this->db->query('SELECT * FROM categories ORDER BY order_by ASC')->result_object();
        $html  = '';
        foreach($data AS $el=>$le)
        {
            if($id == $le->id)
            {
                $selected = 'selected="selected"';
            }else{
                $selected = '';
            }
            $html .= '<option value="'.$le->id.'" '.$selected.'>'.$le->title.'</option>';
        }
        return $html;
    }
    
    public function editNewsDb($id,$img)
    {
        $namePro            = $this->getParamString('namePro');
        $title_ascii        = $this->get_ascii($namePro);
        $nameProSeo         = $this->getParamString('nameProSeo');
        $nameProSeo_ascii   = $this->get_ascii($nameProSeo);
        $tags               = $this->getParamString('hidden-undefined');
        $content            = addslashes($_POST['content']);
        $DesProSeo         = $this->getParamString('DesProSeo');
        $DesProSeo_ascii   = $this->get_ascii($DesProSeo);
        
        $id_c = $this->getParamInt('cateId');
        @$c_type = $this->db->query('SELECT id_type FROM categories WHERE id = '.$id_c)->row_object()->id_type;
        
        $data = array(
                    'tags'              => $tags,
                    'title'             => $namePro,
                    'title_ascii'       => $title_ascii,
                    'title_seo'         => $nameProSeo,
                    'title_seo_ascii'   => $nameProSeo_ascii,
                    'description_seo'         => $DesProSeo,
                    'description_seo_ascii'   => $DesProSeo_ascii,
                    'content'           => $content,
                    'brief'           => $this->getParamString('brief'),
                    'id_c'              => $id_c
                    );
        if(!in_array($img, array(null, '', '0')))
        {
            $data['img'] = $img;
            // X? lý thêm ph?n xóa ?nh
        }
        
        $this->db->update('content', $data,"id = ".$id."");
        $n_link = $this->db->query('SELECT n_link FROM content WHERE id = '.$id)->row_object()->n_link;

        $link = array(
                'link' => 'post-'.$this->replace_url($title_ascii).'/'.$this->id_encode($id).'.html',
                'title' => 'Bài viết '.$namePro,
                'type_link' => 4,
                'type_cate' => $c_type,
                );
        $this->db->update('link_all', $link,"n_link = '".$n_link."'");
        return 200;
    }
    
    public function getNamecate()
    {
        return $this->db->query("SELECT * FROM categories ORDER BY order_by ASC")->result_object();
    }
    
    public function addNewsDb($img,$id_parent)
    {
        $namePro            = $this->getParamString('namePro');
        $title_ascii        = $this->get_ascii($namePro);
        $nameProSeo         = $this->getParamString('nameProSeo');
        $nameProSeo_ascii   = $this->get_ascii($nameProSeo);
        $tags               = $this->getParamString('hidden-undefined');
        $DesProSeo         = $this->getParamString('DesProSeo');
        $DesProSeo_ascii   = $this->get_ascii($DesProSeo);
        
        $id_c = $this->getParamInt('cateId');
        $c_type = $this->db->query('SELECT id_type FROM categories WHERE id = '.$id_c)->row_object()->id_type;
        
        $data = array(
                    'tags'              => $tags,
                    'title'             => $namePro,
                    'title_ascii'       => $title_ascii,
                    'title_seo'         => $nameProSeo,
                    'title_seo_ascii'   => $nameProSeo_ascii,
                    'img'               => $img,
                    'description_seo'         => $DesProSeo,
                    'description_seo_ascii'   => $DesProSeo_ascii,
                    'content'           => addslashes($_POST['content']),
                    'id_c'              => $id_c,
                    'id_c_parent'       => $id_parent,
                    'brief'           => $this->getParamString('brief'),
                    'n_link'            => 'product_4_link'.$title_ascii
                    );
        
        $this->db->insert('content', $data);
        $id = $this->db->insert_id();
        $link = array(
                'link' => 'post-'.$this->replace_url($title_ascii).'/'.$this->id_encode($id).'.html',
                'title' => 'Bài viết '.$namePro,
                'type_link' => 4,
                'type_cate' => $c_type,
                'n_link'    => 'product_4_link'.$title_ascii
                );
        $this->db->insert('link_all', $link);
        return 200;
    }
    
    
    public function getNewsEdit($id)
    {
        $var = $this->db->query('SELECT * FROM content WHERE del = 0 AND id = '.$id)->row_object();
        $exp_tags = explode(',',$var->tags);
        $tags = '';
        foreach($exp_tags AS $el)
        {
            $tags .= '"'.$el.'",';
        }
        $var->tags_show = substr($tags, 0, -1);
        return $var;
    }
    
    public function deleteProductDB($user)
    {
        $id = $this->getParamInt('id');
        $n_link = $this->db->query('SELECT n_link FROM content WHERE id = '.$id)->row_object()->n_link;
        $this->db->delete('link_all',"n_link = '".$n_link."'");
        $this->db->delete('content',"id = ".$id);
        return 200;
    }
    
    public function danhmucGroupotp($selected = '', $id)
    {
        $html = '';
    	$gRs = $this->db->query("SELECT id AS gid,title AS gtitle,parent AS gparent FROM categories WHERE id = '".$id."' ORDER BY order_by ASC")->result_array();
        if($gRs)
        {
            $child = '|--';
            foreach ($gRs As $gel)
        	{
    	    		$html .= ' <optgroup label="' . $gel['gtitle'] .'" style="font-weight: bold;color: #16708A">
                    ';
                    $html .= $this->subdanhmucGroupotp($selected, $gel['gid'], $child);
                    $html .= '
                    </optgroup>
                    ';
        	}
        }
        return $html;
    }
    private function subdanhmucGroupotp($selected, $parent,$child)
    {
        $child = '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'.$child;
        $htmls = '';
        $gGs = $this->db->query("SELECT id AS sGid,parent AS sGparent, title AS sGtitle FROM categories WHERE parent = '".$parent."' ORDER BY id ASC")->result_array();
        foreach ($gGs As $key=>$val)
		{
		      if($selected == $val['sGid'])
               {
                    $selecteds = 'selected="selected"';
               }else{
                    $selecteds = '';
               }
               $check_parent = $this->db->query('SELECT id FROM categories WHERE parent = '.$val['sGid'].' AND id_type = 2')->num_rows();
                if($check_parent > 0)
                {
                    $htmls .= ' <optgroup label="' . $child .' ' . $val['sGtitle'] .'" style="font-weight: bold;color: #16708A">
                    ';
                    $htmls .= $this->subdanhmucGroupotp($selected, $val['sGid'], $child);
                    $htmls .= '</optgroup>';
                }else{
                    $htmls .= '<option value="' . $val['sGid'] . '" '.$selecteds;
                    $htmls .= ($selected == $val['sGid']) ? 'selected="selected"' : '';
    				$htmls .= '>' . $child .' '. $val['sGtitle'] . '</option>';
                    $htmls .= $this->subdanhmucGroupotp($selected, $val['sGid'], $child);
                }
		}
        return $htmls;
    }
    
    
}